package com.example.testblog.web.admin;


import com.example.testblog.entity.Tag;
import com.example.testblog.service.TagsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
@RequestMapping(value = "/admin")
public class TagsController {

    @Autowired
    private TagsService tagsService;

    @GetMapping("/tags")
    public  String tags(@PageableDefault( size=3, sort={"id"},direction=Sort.Direction.DESC)Pageable pageable, Model model)
    {
        model.addAttribute("page",tagsService.listTags(pageable));
        System.out.println("标签控制层出现"+model);
        return "admin/tags";
    }

    @GetMapping("/tags/input")
    public  String Tags_input()
    {
        return "admin/tags_input";
    }


    @PostMapping("/tags")
    public  String Tags_save(Tag tag, RedirectAttributes redirectAttributes)
    {
              Tag tag1=tagsService.saveTags(tag);
              if (tag1==null)
              {

                  redirectAttributes.addFlashAttribute("message","标签增加成功");
              }
              else
              {
                  redirectAttributes.addFlashAttribute("message","标签增加成功");
              }
              return "redirect:/admin/tags";
    }
    @GetMapping("/admin/tages/{id}/delete")
    public String Tags_Delete(Tag tag)
    {

        tagsService.TagesDelete(tag);
        return "redirect:/admin/tags";
    }


}
